﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace QQRobot
{
    class Http
    {


        public static byte[] Post(string url, Dictionary<string, string> args, Dictionary<string, string> head,ref CookieContainer cookie)
        {

            HttpWebRequest request = CreateRequest(url, ref cookie);
            SetHead(request, head);
            string postData = ParamsBuild(args);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postData.Length;

            Stream rStream = request.GetRequestStream();
            StreamWriter wStream = new StreamWriter(rStream, Encoding.GetEncoding("gb2312"));
            wStream.Write(postData);
            wStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            byte[] datas = new byte[4096];
            int actual = stream.Read(datas, 0, 4096);
            stream.Close();

            return datas.Take(actual).ToArray();

        }

        public static byte[] Get(string url, Dictionary<string, string> args, Dictionary<string, string> head, ref CookieContainer cookie)
        {

            HttpWebRequest request = CreateRequest(url + "?" + ParamsBuild(args), ref cookie);

            SetHead(request, head);

            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            byte[] datas = new byte[4096];
            int actual = stream.Read(datas, 0, 4096);
            stream.Close();
            
            return datas.Take(actual).ToArray();
           
        }


        private static HttpWebRequest CreateRequest(string url, ref CookieContainer cookie)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            if (cookie.Count == 0)
            {
                request.CookieContainer = new CookieContainer();
                cookie = request.CookieContainer;
            }
            else
            {
                request.CookieContainer = cookie;
            }
            request.AllowAutoRedirect = false;
            return request;
        }

        private static void SetHead(HttpWebRequest request, Dictionary<string, string> head)
        {
            if (head != null)
            {
                foreach(KeyValuePair<string, string> k in head)
                {
                    switch (k.Key.ToLower())
                    {
                        case "referer":
                            request.Referer = k.Value;
                            break;
                        case "host":
                            request.Host = k.Value;
                            break;
                        default:
                            request.Headers[k.Key] = k.Value;
                            break;
                    }
                }
            }
        }

        private static string ParamsBuild(Dictionary<string, string> args)
        {
            if (args == null || args.Count == 0) return "";
            StringBuilder stringBuilder = new StringBuilder();
            foreach(KeyValuePair<string,string> k in args)
            {
                stringBuilder.Append(k.Key);
                stringBuilder.Append('=');
                stringBuilder.Append(k.Value);
                stringBuilder.Append('&');

            }
            return stringBuilder.ToString();
        }
    }
}
